package com.cubeofnine.sugar;

import java.util.Vector;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cubeofnine.sugar.business.BusinessImpt;
import com.cubeofnine.sugar.business.ContactsBusiness;
import com.cubeofnine.sugar.service.element.EntryResult;

public class ContentDetailActivity extends ActivityImpt {
	public static final String MODULE_NAME = "module_name";
	public static final String MODULE_ID = "module_id";
	public static final String ACCOUNT_ID = "account_id";

	private static TextView headerTitle = null;
	private static ContactsBusiness contactsBusiness = null;
	private static EntryResult entryResult = null;

	private static String module_id = "";
	private static String module_name = "";

	// Thread and Handler Variables
	private static Handler handler;
	private Thread thread;

	private static Context context = null;

	public String getClassName() {
		return ContentDetailActivity.class.getName();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.i(getClassName(), "onCreate() : Start");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.content_detail);

		context = this;

		headerTitle = (TextView) findViewById(R.id.header_title);
		headerTitle.setText(R.string.label_loading);

		module_name = getIntentParams().getString(MODULE_NAME);
		module_id = getIntentParams().getString(MODULE_ID);

		try {
			boolean isOnline = isOnline();

			Log.d(getClassName(), "isOnline = " + isOnline);
			if (isOnline) {
				// Create a handler to update the UI
				handler = new Handler();

				// Check if the thread is already running
				thread = (Thread) getLastNonConfigurationInstance();
				if (thread != null && thread.isAlive()) {
					initProgressDialog(null);
				} else {
					onLoadContacts();
				}
			} else {
				alertDialog(R.string.msg_no_connection);
			}

		} catch (Exception e) {
			Log.e(getClassName(), "onCreate()", e);
			alertDialog(R.string.msg_application_error);
		}

		Log.i(getClassName(), "onCreate() : End");
	}

	public void onDestroy() {
		Log.i(getClassName(), "onDestroy() : Start");

		if (dialog != null && dialog.isShowing()) {
			dialog.dismiss();
			dialog = null;
		}

		Log.i(getClassName(), "onDestroy() : End");
		super.onDestroy();
	}

	// Save the thread
	@Override
	public Object onRetainNonConfigurationInstance() {
		Log.d(getClassName(), "onRetainNonConfigurationInstance()");
		return thread;
	}

	static public class ContentDetailActivityThread extends Thread {
		public static String getClassName() {
			return ContentDetailActivityThread.class.getName();
		}

		@Override
		public void run() {
			Log.i(getClassName(), "run() : Start");

			try {
				Vector<String> column = new Vector<String>();
				column.add("id");
				column.add("first_name");
				column.add("last_name");

				contactsBusiness = new ContactsBusiness(context);
				entryResult = contactsBusiness.getEntry(module_name, module_id,
						column);
			} catch (Exception e) {
				Log.e(getClassName(), "Connection Error", e);
				errorMessage = context.getString(R.string.msg_connection_error);
			} finally {
				Log.d(getClassName(), "Finally");
				handler.post(new ContentDetailActivityRunnable());
			}

			Log.i(getClassName(), "run() : End");
		}

	}

	static public class ContentDetailActivityRunnable implements Runnable {
		public static String getClassName() {
			return ContentDetailActivityRunnable.class.getName();
		}

		public void run() {
			Log.i(getClassName(), "run() : Start");
			Log.d(getClassName(), "errorMessage = " + errorMessage);

			if (errorMessage != null && !"".equals(errorMessage.trim())) {
				((ActivityImpt) context).alertDialog(errorMessage);
			} else {
				((ContentDetailActivity) context).genContent();
			}

			dialog.dismiss();

			Log.i(getClassName(), "run() : End");
		}
	}

	public void onLoadContacts() {
		Log.i(getClassName(), "onLoadContacts() : Start");

		initProgressDialog(null);

		thread = new ContentDetailActivityThread();
		thread.start();

		Log.i(getClassName(), "onLoadContacts() : End");
	}

	public void genContent() {
		LinearLayout root = (LinearLayout) findViewById(R.id.body_root_linear_layout);
		root.removeAllViewsInLayout();

		if (entryResult != null) {
			if (BusinessImpt.MODULE_CONTACTS.equals(module_name)) {
				ContentContactDynamicLayout layout = new ContentContactDynamicLayout();
				layout.genContent(this, headerTitle, root, entryResult,
						module_name, module_id);
			} else if (BusinessImpt.MODULE_ACCOUNTS.equals(module_name)) {
				ContentAccountDynamicLayout layout = new ContentAccountDynamicLayout();
				layout.genContent(this, headerTitle, root, entryResult,
						module_name, module_id);
			}
		}
	}
	
	public void onClickHomeButton(View view) {
		try {
			view.startAnimation(AnimationUtils.loadAnimation(this,
					R.anim.image_click));
			createMenuPopup(R.string.label_home_menu,
					new String[] { MENU_LOGOUT });
		} catch (Exception e) {
			Log.e(ActivityImpt.class.getName(), "Error", e);
		}
	}
}
